package com.carlinfo.stu_ques.users.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.users.dao.ILevelDao;
import com.carlinfo.stu_ques.users.dao.IStudentDao;
import com.carlinfo.stu_ques.users.dao.impl.LevelDaoImpl;
import com.carlinfo.stu_ques.users.dao.impl.StudentDaoImpl;
import com.carlinfo.stu_ques.users.pojo.Level;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.IUsersService;

public class UsersServiceImpl implements IUsersService
{
	IStudentDao studentDao = new StudentDaoImpl();
	ILevelDao levelDao = new LevelDaoImpl();

	@Override
	public int saveOneStudentService(Students stu)
	{
		int id = this.studentDao.saveOneDao(stu);
		return id;
	}

	@Override
	public void updateOneStudentService(Students stu)
	{
		this.studentDao.updateOneDao(stu);
	}

	@Override
	public void deleteOneStudentService(Students stu)
	{
		this.studentDao.deleteOneDao(stu);
	}

	@Override
	public List<Students> selectListStudentService()
	{
		return this.studentDao.selectListDao();
	}

	@Override
	public Students selectOneStudentService(Map<String, Object> condMap)
	{
		return this.studentDao.selectOneDao(condMap);
	}

	@Override
	public String title()
	{
		/* id,姓名,身高,创建时间,更新时间,爱好 */
		return "id\t姓名\t性别\t毕业院校\t\t分数\t总次数\t答对次数\t答错次数\t\t准确率\t今日正确\t今日答错\t今日正确率\t创建时间\t\t\t更新时间";
	}

	@Override
	public int selectStudentMaxIdService()
	{
		return this.studentDao.selectMaxIdDao();
	}

	public boolean askIsStudentService(Students students)
	{
		/* 如果学生为null */
		if (students == null)
		{
			return false;
		}
		/* 找出所有学生中回答问题次数最多的 */
		int askCount = 0;
		/* flag:相当于为maxAskCount上了一把锁 */
		boolean flag = true;
		int maxAskCount = 0;
		/* 断言:true:全体同同学所有的回答次数是一样的;随记抽取 */
		boolean askFlag = true;
		List<Students> studentsList = this.selectListStudentService();
		for (Iterator iterator = studentsList.iterator(); iterator.hasNext();)
		{
			Students stuTemp = (Students) iterator.next();
			if (stuTemp.getTodayCount() > askCount)
			{
				askCount = stuTemp.getTotalConut();
			}
			if (maxAskCount == 0 && flag)
			{
				/* 此代码只执行一次 */
				maxAskCount = stuTemp.getTodayCount();
				flag = false;
			}
			if (maxAskCount != stuTemp.getTodayCount())
			{
				/* 所有的学生回答次数已经不一样了 */
				askFlag = false;
			}
		}
		/* 判断一下,如果askCount是最大值 */
		if (students.getTotalConut() < askCount || askCount == 0 || askFlag)
		{
			return true;
		}
		return false;

	}

	/* =================等级================= */
	@Override
	public int saveOneLevelService(Level level)
	{
		return levelDao.saveOneDao(level);
	}

	@Override
	public void updateOneLevelService(Level level)
	{
		this.levelDao.updateOneDao(level);
	}

	@Override
	public void deleteOneLevelService(Level level)
	{
		this.levelDao.deleteOneDao(level);
	}

	@Override
	public List<Level> selectListLevelService()
	{
		return this.levelDao.selectListDao();
	}

	@Override
	public Level selectOneLevelService(Map<String, Object> condMap)
	{
		return this.levelDao.selectOneDao(condMap);
	}

	@Override
	public String levelTitle()
	{
		return "id\t姓名\t\t初始分数\t\t结束分数\t\t创建时间\t\t\t更新时间";
	}
	public void setStudentDao(IStudentDao studentDao)
	{
		this.studentDao = studentDao;
	}
	public void setLevelDao(ILevelDao levelDao)
	{
		this.levelDao = levelDao;
	}

}
